<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;


    public function getUserAgent(){
        if( strpos($_SERVER['HTTP_USER_AGENT'], 'PTime.IOSWeb') !== false):
            $userAgent = "ptimeios";
        elseif( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false):
            $userAgent = "wechat";
        elseif( strpos($_SERVER['HTTP_USER_AGENT'], 'PTime.AndroidWeb') !== false):
            $userAgent = "ptimeandroid";
        elseif( strpos($_SERVER['HTTP_USER_AGENT'], 'MQQBrowser')!== false):
            $userAgent = "mqq";
        else:
            $userAgent = "other";
        endif;
        return $userAgent;
    }


    /**
     * PHP CURL get请求
     *
     * @param uri     请求地址
     * @return
     */
    public function curl_get( $uri , $token = "" , $host = "" , $auth = "" ){
        $time = time();
        $header = array(
            'clientdate: '.$time ,
            'ptimeauth: '.$token ,
            );
        if( $auth != "" ){
            $header[] = 'authorization: '.$auth;
        }
        if( $host != "" ){
            $header[] = 'Host: '.$host;
        }
        $temp_uri = $uri;
        $ch = curl_init();
        curl_setopt ( $ch, CURLOPT_URL, $temp_uri );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        if( $token != "" || $auth != '' ){
            curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
        }
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    /**
     * PHP CURL 模块发送post请求
     *
     * @param uri     请求地址
     * @param data    所需发送的数据
     * @return
     */
    public function curl_post( $uri , $data ){
        $temp_uri = $uri;
        $temp_data = http_build_query($data);
        $ch = curl_init();
        curl_setopt ( $ch, CURLOPT_URL, $temp_uri );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, array("Expect:") );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $temp_data );
        $temp_post_result = curl_exec ( $ch );
        curl_close ( $ch );
        return $temp_post_result;
    }

}
